<?php
/**
 * 权限模型
 */
class Auth_model extends MY_Model
{
	private $table = 'authgroup';
	
	function __construct()
	{
		parent::__construct();
	}

	/**
	 * 获取权限组列表
	 * @DateTime 2017-07-17
	 * @author zyl
	 * @return   [type]     [description]
	 */
	function getAuthList($where=array('status'=>1)){
		// $where['status'] = 1;
		$list = $this->hw_getList($this->table,$where);
		return $list;
	}

	/**
	 * 根据id获取信息
	 * @DateTime 2017-07-17
	 * @author zyl
	 * @param    [type]     $id [description]
	 * @return   [type]         [description]
	 */
	function getAuthById($id){

		$where = array('id'=>$id,'status'=>1);	
		$auth = $this->hw_getRecord($this->table,$where);
		return $auth;

	}

	/**
	 * 获取当前用户权限列表
	 * @DateTime 2017-07-17
	 * @author zyl
	 * @param    [type]     $auth_id [description]
	 * @return   [type]              [description]
	 */
	function getSessionAuth($auth_id){
		$where['id'] = $auth_id;
		$select = 'authlist';
		$res = $this->hw_getRecord($this->table,$where,$select);
		return json_decode($res['authlist'],true);
	}

	/**
	 * 获取模块列表
	 * @DateTime 2017-07-17
	 * @author zyl
	 * @return   [type]     [description]
	 */
	// function getSessionAuthname(){
	// 	return $this->hw_getList('authlist',$where,'*','id desc');
	// }


	/**
	 * 处理编辑/添加权限组
	 * @DateTime 2017-07-17
	 * @author zyl
	 * @param    [type]     $post [description]
	 * @return   [type]           [description]
	 */
	function handleAddAuth($post){

		if($post['id']==0){
			//新增
			$return = $this->addAuth($post);
		}else{
			//编辑
			$return = $this->editAuth($post);
		}
		return $return;
	}

	/**
	 * 添加权限组
	 * @DateTime 2017-07-17
	 * @author zyl
	 * @param    [type]     $post [description]
	 */
	function addAuth($post){
		$data = array(
				'authlist' => json_encode($post['authlist']),
				'name'     => $post['name'],
			);
		$ret = $this->hw_Insert($this->table,$data);
		if($ret){
			$return = array('code'=>1,'msg'=>'添加成功');
		}else{
			
			$return = array('code'=>0,'msg'=>'添加失败');
		}
		//添加操作日志
		$this->logMdl->addOperationLog($return['code'],1,$ret,$this->table,'权限组');
		return $return;
	}

	/**
	 * 编辑权限组
	 * @DateTime 2017-07-17
	 * @author zyl
	 * @param    [type]     $post [description]
	 * @return   [type]           [description]
	 */
	function editAuth($post){
		$where['id'] = $post['id'];
		$data = array(
				'authlist' => json_encode($post['authlist']),
				'name'     => $post['name'],
			);
		$res = $this->hw_Update($this->table,$data,$where);

		if($res){
			$return = array('code'=>1,'msg'=>'编辑成功');
		}else{
			
			$return = array('code'=>0,'msg'=>'编辑失败');
		}
		//添加操作日志
		$this->logMdl->addOperationLog($return['code'],2,$res,$this->table,'权限组');
		return $return;
	}

	/**
	 * 获取模块列表
	 * @DateTime 2017-07-17
	 * @author zyl
	 * @return   [type]     [description]
	 */
	function getModuleList(){
		$where['status'] = 1;
		$list = $this->hw_getList('authlist',$where);
		return $list;
	}

	/**
	 * 更新信息(多条)
	 * @DateTime 2017-07-17
	 * @author zyl
	 * @return   [type]     [description]
	 */
	function updateAuth_in($data,$ids){
		$res = $this->hw_Update_in($this->table,$data,'id',$ids);
		if($res){
			$return = array('code'=>1,'msg'=>'编辑成功');
		}else{
			$return = array('code'=>0,'msg'=>'编辑失败');
		}
		//添加操作日志
		$this->logMdl->addOperationLog($return['code'],2,implode(',',$ids),$this->table,'权限组');
		return $return;
		// return $this->hw_Update_in($this->table,$data,'id',$ids);
	}
	
}